
const Controller = require('./base_controller')

class AccountSerController extends Controller {
  constructor (...props) {
    super(...props)
    this.rule = {
      accountId: 'int', 
      serviceId: 'int',
      extend: 'json'
    }
  }
  //查询
  async index (ctx) {
    let { userId } = ctx.params
    try {
      let serveList = await ctx.service.accountSerService.index(userId)
      this.success({data: serveList.rows})
    } catch (e) {
      this.error({
        msg: '查询失败!',
        data: e
      })
    }
  }
  //创建
  async create (ctx) {
    let { body } = ctx.request
    try {
      ctx.validate(this.rule)
      await ctx.service.accountSerService.create(body)
      this.success({
        msg: '创建成功!',
        data: body
      })
    } catch (e) {
      let msg = (e && e.invalid_param) 
        ? '参数错误!' : '创建失败,请稍后再试!'
      this.error({
        msg: msg,
        data: e
      })
    }
  }
  //修改
  async update (ctx) {
    let { params, request: { body } } = ctx
    try {
      ctx.validate(this.rule)
      console.log('in')
      let props = Object.assign({}, params, body)
      console.log(props)
      await ctx.service.accountSerService.update(props)
      console.log('out')
      this.success({
        msg: '修改成功!',
        data: props
      })
    } catch (e) {
      let msg = (e && e.code)
        ? '参数错误!' : '修改失败，请稍后再试!'
      this.error({
        msg: msg,
        data: e
      })
    }
  }
  //删除
  async delete (ctx) {
    let { id } = ctx.params
    try {
      await ctx.service.accountSerService.delete(id)
      this.success({
        data: { id }
      })
    } catch (e) {
      this.error({
        msg: '删除失败!',
        data: e
      })
    }
  }
}

module.exports = AccountSerController
